import React from 'react';
import { Card } from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import Search from './components/Search';
import ExportButton from '@/components/ExportButton';
import { flowCirculationPageList, flowCirculationExport } from '@/services/Flow';
import columns from './columns';

class Circulation extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      params: {}
    }
    this.proTableRef = React.createRef();
  }

  /* 获取注销列表 */
  getPageList = async (currentParams) => {
    try {
      const params = this.state.params;   
      const res = await flowCirculationPageList({
        ...params,
        ...currentParams
      })
      return {
        data: res.data.map(function (item, index) {
          item.index = index + '-' + new Date().getTime();
          return item
        }),
        total: res.totalSize,
        success: true
      }
    } catch (e) {
      console.error(e)
    }
  }

  /* 监听查询事件 */
  onSearch = (data) => {
    const params = this.state.params;
    this.setState({params: {...params, ...data}});
    this.proTableRef.current.reload();
  }

  /* 刷新事件 */
  onReload = () => {
    this.setState({params: {}});
    this.proTableRef.current.reload();
  }

  render() {
    const proTableRef = this.proTableRef;
    const {onSearch, onReload} = this;
    const params = this.state.params;
    return (
      <PageContainer>
        <Card
          title='统计列表'
          bodyStyle={{paddingBottom: 0, paddingTop: 30}}
          bordered={false}>
          <Search search={onSearch} reset={onReload} />
        </Card>
        <ProTable
          actionRef={proTableRef}
          rowKey='index'
          headerTitle={
            <ExportButton query={params} request={flowCirculationExport} />
          }
          search={false}
          scroll={{ x: true }}
          columns={columns}
          request={this.getPageList} />
      </PageContainer>
    );
  }
}

export default Circulation;
